package com.pgybook.front.service.impl;

import com.google.common.base.Joiner;
import com.pgybook.db.entity.domain.*;
import com.pgybook.db.entity.mapper.CityDistrictMapper;
import com.pgybook.db.entity.mapper.CityMapper;
import com.pgybook.db.entity.mapper.CustomerDeliverAddressMapper;
import com.pgybook.db.entity.mapper.ProvinceMapper;
import com.pgybook.front.action.vo.MemberAddressVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: feeling
 * Date: 13-11-21
 * Time: 下午6:32
 */
@Service
public class MemberAddressServiceImpl implements com.pgybook.front.service.MemberAddressService {

    private static final Logger log = LoggerFactory.getLogger(MemberAddressServiceImpl.class);

    @Override
    public List<CustomerDeliverAddress> list(Long custId) {
        CustomerDeliverAddressEx ex = new CustomerDeliverAddressEx();
        ex.createCriteria().andCustidEqualTo(custId);
        List<CustomerDeliverAddress> list = addressDao.selectByExample(ex);
        return list;
    }

    @Override
    public boolean remove(Integer addressId) {
        return 0 < addressDao.deleteByPrimaryKey(addressId);
    }

    @Override
    public void saveAddress(Long custId, MemberAddressVO vo) {
        Province province = provinceDao.selectByPrimaryKey(vo.getProvince());
        City city = cityDao.selectByPrimaryKey(vo.getCity());
        CityDistrict cityDistrict = cityDistrictDao.selectByPrimaryKey(vo.getCityDistrict());
        CustomerDeliverAddressEx ex = new CustomerDeliverAddressEx();

        CustomerDeliverAddress add = new CustomerDeliverAddress();
        add.setCustaddressid(custId.intValue());
        add.setContact(vo.getUsername());
        add.setCrttime(new Date());
        add.setIsdefault(vo.isDefaultAddress());
        add.setZipcode(vo.getYouzheng());
        add.setPhone(vo.getNum());
        String address = Joiner.on(" ").join(province.getName(), city.getCityname(), cityDistrict.getCdname(), vo.getAddress());
        log.debug("生成的地址为{}", address);
        add.setAddress(address);
        //设置默认地址
        Integer addressId = addressDao.insert(add);
        if (addressId != null && add.getIsdefault()) {
            setAddressDefault(custId, addressId);
        }
    }


    @Override
    public boolean setAddressDefault(Long custId, Integer addressId) {
        CustomerDeliverAddress address = addressDao.selectByPrimaryKey(addressId);
        //设置默认地址
        if (address != null && !address.getIsdefault()) {
            address.setIsdefault(true);
            addressDao.updateByPrimaryKey(address);
            //DetachedCriteria dc = DetachedCriteria.forClass(CustomerDeliverAddress.class);
            //找出ID和目标Id不一样的而且为默认Id的地址 取消其默认设置
            CustomerDeliverAddressEx ex = new CustomerDeliverAddressEx();
            ex.createCriteria().andCustidEqualTo(custId).andIsdefaultEqualTo(false)
                    .andCustaddressidNotEqualTo(addressId);
            List<CustomerDeliverAddress> list = addressDao.selectByExample(ex);
            for (CustomerDeliverAddress add : list) {
                add.setIsdefault(false);
                addressDao.updateByPrimaryKey(add);
            }
            return true;
        }
        return false;

    }

    @Autowired
    CustomerDeliverAddressMapper addressDao;

    @Autowired
    CityMapper cityDao;

    @Autowired
    CityDistrictMapper cityDistrictDao;

    @Autowired
    ProvinceMapper provinceDao;

}
